package com.ciwong.epaper.modules.me.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.text.TextPaint;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EpaperConstant;
import com.ciwong.epaper.modules.me.bean.Service;
import com.ciwong.epaper.modules.me.ui.mall.SlideShowView;
import com.ciwong.epaper.modules.me.util.MeJumpManager;
import com.ciwong.epaper.util.EConstants;
import com.ciwong.libs.utils.CWLog;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by neo on 2016/7/6.
 */
public class MicroMallHomePageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

	private List<Service> mServiceList   = new ArrayList<>();
	private List<Service> mPromotionList = new ArrayList<>();
	private Activity mActivity;
	private static final String TAG = "MicroMallHomePageAdapte";

	public MicroMallHomePageAdapter(Activity activity) {
		mActivity = activity;
	}

	@Override
	public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		if (viewType == 0 && mPromotionList.size() > 0) {
			View view = View.inflate(parent.getContext(), R.layout.item_micro_mall_top_sub, null);
			OnSaleViewHolder onSaleViewHolder = new OnSaleViewHolder(view);
			return onSaleViewHolder;
		} else {
			View view = View.inflate(parent.getContext(), R.layout.item_micro_mall, null);
			return new ServiceViewHolder(view);
		}
	}

	@Override
	public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {

		if (position == 0 && mPromotionList != null && mPromotionList.size() > 0) {
			OnSaleViewHolder onSaleViewHolder = (OnSaleViewHolder) holder;
			onSaleViewHolder.slide_show_view.setServices(mPromotionList);

		} else {

			final ServiceViewHolder serviceViewHolder = (ServiceViewHolder) holder;
			int offset = 0;
			if (mPromotionList != null && mPromotionList.size() > 0) {
				offset = 1;
			}

			final Service service = mServiceList.get(position - offset);
			serviceViewHolder.btn_item_buy_service.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					MeJumpManager.jumpBuyService(mActivity, R.string.go_back, service.getId(), service.getSign(), EpaperConstant.REQUEST_CODE_CLASS_JUMP_BUY_SERVICE_PAGE, "");
				}
			});

			serviceViewHolder.tv_service_name.setText(service.getName());
			serviceViewHolder.tv_service_name.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
				@Override
				public void onGlobalLayout() {
					serviceViewHolder.tv_service_name.getViewTreeObserver().removeOnGlobalLayoutListener(this);
					if (serviceViewHolder.tv_service_name.getLineCount() >1) {
						RelativeLayout.LayoutParams tvServiceNameLayouts = (RelativeLayout.LayoutParams) serviceViewHolder.tv_service_name.getLayoutParams();
						tvServiceNameLayouts.topMargin = mActivity.getResources().getDimensionPixelSize(R.dimen.dp_1);
						serviceViewHolder.tv_service_name.setLayoutParams(tvServiceNameLayouts);
					} else {
						RelativeLayout.LayoutParams tvServiceNameLayouts = (RelativeLayout.LayoutParams) serviceViewHolder.tv_service_name.getLayoutParams();
						tvServiceNameLayouts.topMargin = mActivity.getResources().getDimensionPixelSize(R.dimen.dp_8);
						serviceViewHolder.tv_service_name.setLayoutParams(tvServiceNameLayouts);
					}
				}
			});



			String logo = mServiceList.get(position - offset).getLogo();
			logo = TextUtils.isEmpty(logo) ? "drawable://" + R.mipmap.ic_launcher
					: logo;
			ImageLoader.getInstance().displayImage(logo, serviceViewHolder.iv_service_logo,
					EConstants.getDefAvatarCircleBuilder());
			final int finalOffset = offset;
			//            if (service.getSign() == 1)
			{
				serviceViewHolder.imgFreeView.setVisibility(View.GONE);
			}
			//暂时隐藏
			//service.getPrice() == 0 ? View.VISIBLE :
			//            serviceViewHolder.suit_area2.setText(service.getArea());

			if (service.getPeriod() != null && !"".equals(service.getPeriod())) {
				serviceViewHolder.suit_period_time.setText(service.getPeriod());
				serviceViewHolder.suit_period_time.setVisibility(View.VISIBLE);
				serviceViewHolder.suit_period.setVisibility(View.VISIBLE);
			} else {
				serviceViewHolder.suit_period_time.setVisibility(View.GONE);
				serviceViewHolder.suit_period.setVisibility(View.GONE);
			}


			serviceViewHolder.handle_item_service.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {

					MeJumpManager.jumpBuyService(mActivity, R.string.go_back, service.getId(), service.getSign(), EpaperConstant.REQUEST_CODE_CLASS_JUMP_BUY_SERVICE_PAGE, "");
				}
			});

		}
	}



	@Override
	public int getItemCount() {
		//如果有促销的服务就加头部Item展示促销服务
		if (mPromotionList != null && mPromotionList.size() > 0) {
			return mServiceList.size() + 1;
		} else {
			return mServiceList.size();
		}
	}

	@Override
	public int getItemViewType(int position) {
		//一定要复写这个函数，要不然返回都是0
		//        Log.d("personal", "############getItemViewType#################"+position);
		return position;
	}

	static class OnSaleViewHolder extends RecyclerView.ViewHolder {
		//     public ItemMicroMallTop item_micro_mall_top;
		public SlideShowView slide_show_view;

		public OnSaleViewHolder(View itemView) {
			super(itemView);

			slide_show_view = (SlideShowView) itemView.findViewById(R.id.slide_show_view);

		}
	}


	static class ServiceViewHolder extends RecyclerView.ViewHolder {
		public ImageView iv_service_logo, imgFreeView;
		public TextView tv_service_name;
		public TextView suit_period_time, suit_period;
		public View      handle_item_service;
		public ImageView btn_item_buy_service;

		public ServiceViewHolder(View itemView) {
			super(itemView);

			btn_item_buy_service = (ImageView) itemView.findViewById(R.id.btn_item_buy_service);
			iv_service_logo = (ImageView) itemView.findViewById(R.id.iv_service_logo);
			tv_service_name = (TextView) itemView.findViewById(R.id.tv_service_name);
			//            suit_area2 = (TextView) itemView.findViewById(R.id.suit_area2);
			suit_period_time = (TextView) itemView.findViewById(R.id.suit_period_time);
			suit_period = (TextView) itemView.findViewById(R.id.suit_period);
			handle_item_service = itemView.findViewById(R.id.handle_item_service);
			imgFreeView = (ImageView) itemView.findViewById(R.id.img_item_free);
		}
	}

	/**
	 * 添加数据集合
	 *
	 * @param services 服务信息集合
	 */
	public void setServiceList(List<Service> services) {
		if (services != null) {
			this.mServiceList = services;
			notifyDataSetChanged();
		}
	}

	/**
	 * 添加数据集合
	 *
	 * @param services 服务信息集合
	 */
	public void setPromotionServiceList(List<Service> services) {
		if (services != null) {
			this.mPromotionList = services;
			notifyDataSetChanged();
		}
	}
}
